package com.guo.hui.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.guo.hui.dao.blog.TpArticleMapper;
import com.guo.hui.exception.BizException;
import com.guo.hui.model.blog.TpArticle;
import com.guo.hui.page.PageParam;
import com.guo.hui.page.PageResult;
import com.guo.hui.rabbitmq.RabbitService;
import com.guo.hui.service.BlogService;
import com.guo.hui.service.CxfUtilService;

@Controller
@RequestMapping("/test")
public class TestController {

	@Autowired
	private CxfUtilService utilService;
	@Autowired
	private RabbitService rabbitService;
	@Autowired
	private BlogService blogService;
	@Autowired
	private TpArticleMapper articleDao;

	@RequestMapping("/fileload")
	public String fileload() {

		return "test/fileload";
	}


	@ResponseBody
	@RequestMapping("/mqsend")
	public String rabbitmq() {
		String exchange = "testExchange";//// 交换器
		String routeKey = "testQueue";// 队列
		String message = "java学习这一部分其实也算是今天的重点" + "这一部分用来回答很多群里的朋友所问过的问转行到Java的同学";
		// RabbitMessage msg = new RabbitMessage(exchange, routeKey, message);
		// rabbitService.sendMessage(msg);
		return "ok";
	}

	@ResponseBody
	@RequestMapping("/mqreceiver")
	public String mqreceiver() {
		String exchange = "testExchange";//// 交换器
		String routeKey = "testQueue";// 队列
		// String message = "helloword-wee";// 调用的方法
		// RabbitMessage msg = new RabbitMessage(exchange, routeKey);
		// rabbitService.receMessage(msg);
		return "ok";
	}

	@RequestMapping("/mobile")
	public String mobile() {
		String msg = utilService.getMobileInfo("15333319753", null);
		System.out.println(msg);
		return "test/testnav";
	}

	@RequestMapping("/testnav")
	public String testnav() {
		return "test/testnav";
	}

	@RequestMapping("/testnav2")
	public String testnav2(Model mm, String type, @RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
		// Map<String, Object> map = new HashMap<>();
		// map.put("cateid", type);
		// List<TpArticle> list = blogService.selectByMap(map);
		// mm.addAttribute("list", list);
		PageParam pageParm = new PageParam();
		pageParm.setPage(page);
		pageParm.setPageSize(pageSize);
		if (page == 0) {
			pageParm.setPage(1);
		}
		if (pageSize == 0) {
			pageParm.setPageSize(5);
		}
		Map<String, Object> map = new HashMap<>();
		map.put("cateid", type);
		pageParm.setSearchContent(map);
		PageInfo<TpArticle> pagelist = blogService.selectByMapPage(pageParm);
		PageResult result = new PageResult();
		result.setTotal((int) pagelist.getTotal());
		result.setRows(pagelist.getList());
		result.setType(type);
		result.setPageTotal(1);
		if (result.getTotal() > 0) {
			result.setPageTotal(result.getTotal() / pageSize + 1);
		}
		mm.addAttribute("result", result);
		mm.addAttribute("page", pagelist);
		return "test/testnav2";
	}

	@RequestMapping("/login")
	public String login() {
		return "test/login";
	}

	@RequestMapping("/testnav3")
	public String testnav3(Model mm) {
		Map<String, Object> map = new HashMap<>();
		List<TpArticle> list = blogService.selectByMap(map);
		mm.addAttribute("list", list);
		return "test/testnav3";
	}

	@RequestMapping("/artdetail")
	public String artdetail(Model mm, @RequestParam Integer id) {
		TpArticle tpArticle = articleDao.selectById(id);
		mm.addAttribute("tp", tpArticle);
		return "test/artdetail";
	}

	@RequestMapping("/ueditor")
	public String ueditor() {
		return "test/ueditor";
	}

	@RequestMapping("/index")
	public String index() {
		return "test/index";
	}

	@ResponseBody
	@RequestMapping("/check")
	public String check(String username, String password) {
		if ("admin".equals(username)) {
			return "ok";
		} else {
			throw new BizException("账号或密码错误,请重新输入!");
		}

	}

}
